using Microsoft.Win32;
using System;
using System.Diagnostics;
using System.Text;
namespace Extra.Core
{
	public class EmbeddedInstall
	{
		private string instanceName = "SQLEXPRESS";
		private string installSqlDir = "";
		private string installSqlSharedDir = "";
		private string installSqlDataDir = "";
		private string addLocal = "All";
		private bool sqlAutoStart = true;
		private bool sqlBrowserAutoStart = false;
		private string sqlBrowserAccount = "";
		private string sqlBrowserPassword = "";
		private string sqlAccount = "";
		private string sqlPassword = "";
		private bool sqlSecurityMode = false;
		private string saPassword = "";
		private string sqlCollation = "";
		private bool disableNetworkProtocols = true;
		private bool errorReporting = true;
		private string sqlExpressSetupFileLocation = Environment.GetEnvironmentVariable("TEMP") + "\\sqlexpr.exe";
		public string InstanceName
		{
			get
			{
				return this.instanceName;
			}
			set
			{
				this.instanceName = value;
			}
		}
		public string SetupFileLocation
		{
			get
			{
				return this.sqlExpressSetupFileLocation;
			}
			set
			{
				this.sqlExpressSetupFileLocation = value;
			}
		}
		public string SqlInstallSharedDirectory
		{
			get
			{
				return this.installSqlSharedDir;
			}
			set
			{
				this.installSqlSharedDir = value;
			}
		}
		public string SqlDataDirectory
		{
			get
			{
				return this.installSqlDataDir;
			}
			set
			{
				this.installSqlDataDir = value;
			}
		}
		public bool AutostartSQLService
		{
			get
			{
				return this.sqlAutoStart;
			}
			set
			{
				this.sqlAutoStart = value;
			}
		}
		public bool AutostartSQLBrowserService
		{
			get
			{
				return this.sqlBrowserAutoStart;
			}
			set
			{
				this.sqlBrowserAutoStart = value;
			}
		}
		public string SqlBrowserAccountName
		{
			get
			{
				return this.sqlBrowserAccount;
			}
			set
			{
				this.sqlBrowserAccount = value;
			}
		}
		public string SqlBrowserPassword
		{
			get
			{
				return this.sqlBrowserPassword;
			}
			set
			{
				this.sqlBrowserPassword = value;
			}
		}
		public string SqlServiceAccountName
		{
			get
			{
				return this.sqlAccount;
			}
			set
			{
				this.sqlAccount = value;
			}
		}
		public string SqlServicePassword
		{
			get
			{
				return this.sqlPassword;
			}
			set
			{
				this.sqlPassword = value;
			}
		}
		public bool UseSQLSecurityMode
		{
			get
			{
				return this.sqlSecurityMode;
			}
			set
			{
				this.sqlSecurityMode = value;
			}
		}
		public string SysadminPassword
		{
			set
			{
				this.saPassword = value;
			}
		}
		public string Collation
		{
			get
			{
				return this.sqlCollation;
			}
			set
			{
				this.sqlCollation = value;
			}
		}
		public bool DisableNetworkProtocols
		{
			get
			{
				return this.disableNetworkProtocols;
			}
			set
			{
				this.disableNetworkProtocols = value;
			}
		}
		public bool ReportErrors
		{
			get
			{
				return this.errorReporting;
			}
			set
			{
				this.errorReporting = value;
			}
		}
		public string SqlInstallDirectory
		{
			get
			{
				return this.installSqlDir;
			}
			set
			{
				this.installSqlDir = value;
			}
		}
		public bool IsExpressInstalled()
		{
			bool result;
			using (RegistryKey registryKey = Registry.LocalMachine.OpenSubKey("Software\\Microsoft\\Microsoft SQL Server\\", false))
			{
				if (registryKey == null)
				{
					result = false;
					return result;
				}
				string[] subKeyNames = registryKey.GetSubKeyNames();
				if (subKeyNames.Length == 0)
				{
					result = false;
					return result;
				}
				string[] array = subKeyNames;
				for (int i = 0; i < array.Length; i++)
				{
					string text = array[i];
					if (text.StartsWith("MSSQL."))
					{
						using (RegistryKey registryKey2 = registryKey.OpenSubKey(text.ToString() + "\\Setup\\", false))
						{
							if ((string)registryKey2.GetValue("Edition") == "Express Edition")
							{
								result = true;
								return result;
							}
						}
					}
				}
			}
			result = false;
			return result;
		}
		public int EnumSQLInstances(ref string[] strInstanceArray, ref string[] strEditionArray, ref string[] strVersionArray)
		{
			int result;
			using (RegistryKey registryKey = Registry.LocalMachine.OpenSubKey("Software\\Microsoft\\Microsoft SQL Server\\", false))
			{
				if (registryKey == null)
				{
					result = 0;
				}
				else
				{
					string[] subKeyNames = registryKey.GetSubKeyNames();
					if (subKeyNames.Length == 0)
					{
						result = 0;
					}
					else
					{
						int num = 0;
						string[] array = subKeyNames;
						for (int i = 0; i < array.Length; i++)
						{
							string text = array[i];
							if (text.StartsWith("MSSQL."))
							{
								num++;
							}
						}
						strInstanceArray = new string[num];
						strVersionArray = new string[num];
						strEditionArray = new string[num];
						int num2 = 0;
						array = subKeyNames;
						for (int i = 0; i < array.Length; i++)
						{
							string text = array[i];
							if (text.StartsWith("MSSQL."))
							{
								using (RegistryKey registryKey2 = registryKey.OpenSubKey(text.ToString(), false))
								{
									strInstanceArray[num2] = (string)registryKey2.GetValue("");
								}
								using (RegistryKey registryKey3 = registryKey.OpenSubKey(text.ToString() + "\\Setup\\", false))
								{
									strEditionArray[num2] = (string)registryKey3.GetValue("Edition");
									strVersionArray[num2] = (string)registryKey3.GetValue("Version");
								}
								num2++;
							}
						}
						result = num2;
					}
				}
			}
			return result;
		}
		private string BuildCommandLine()
		{
			StringBuilder stringBuilder = new StringBuilder();
			if (!string.IsNullOrEmpty(this.installSqlDir))
			{
				stringBuilder.Append("INSTALLSQLDIR=\"").Append(this.installSqlDir).Append("\"");
			}
			if (!string.IsNullOrEmpty(this.installSqlSharedDir))
			{
				stringBuilder.Append("INSTALLSQLSHAREDDIR=\"").Append(this.installSqlSharedDir).Append("\"");
			}
			if (!string.IsNullOrEmpty(this.installSqlDataDir))
			{
				stringBuilder.Append("INSTALLSQLDATADIR=\"").Append(this.installSqlDataDir).Append("\"");
			}
			if (!string.IsNullOrEmpty(this.addLocal))
			{
				stringBuilder.Append(" ADDLOCAL=\"").Append(this.addLocal).Append("\"");
			}
			if (this.sqlAutoStart)
			{
				stringBuilder.Append(" SQLAUTOSTART=1");
			}
			else
			{
				stringBuilder.Append(" SQLAUTOSTART=0");
			}
			if (this.sqlBrowserAutoStart)
			{
				stringBuilder.Append(" SQLBROWSERAUTOSTART=1");
			}
			else
			{
				stringBuilder.Append(" SQLBROWSERAUTOSTART=0");
			}
			if (!string.IsNullOrEmpty(this.sqlBrowserAccount))
			{
				stringBuilder.Append("SQLBROWSERACCOUNT=\"").Append(this.sqlBrowserAccount).Append("\"");
			}
			if (!string.IsNullOrEmpty(this.sqlBrowserPassword))
			{
				stringBuilder.Append("SQLBROWSERPASSWORD=\"").Append(this.sqlBrowserPassword).Append("\"");
			}
			if (!string.IsNullOrEmpty(this.sqlAccount))
			{
				stringBuilder.Append(" SQLACCOUNT=\"").Append(this.sqlAccount).Append("\"");
			}
			if (!string.IsNullOrEmpty(this.sqlPassword))
			{
				stringBuilder.Append(" SQLPASSWORD=\"").Append(this.sqlPassword).Append("\"");
			}
			if (this.sqlSecurityMode)
			{
				stringBuilder.Append(" SECURITYMODE=SQL");
			}
			if (!string.IsNullOrEmpty(this.saPassword))
			{
				stringBuilder.Append(" SAPWD=\"").Append(this.saPassword).Append("\"");
			}
			if (!string.IsNullOrEmpty(this.sqlCollation))
			{
				stringBuilder.Append(" SQLCOLLATION=\"").Append(this.sqlCollation).Append("\"");
			}
			if (this.disableNetworkProtocols)
			{
				stringBuilder.Append(" DISABLENETWORKPROTOCOLS=1");
			}
			else
			{
				stringBuilder.Append(" DISABLENETWORKPROTOCOLS=0");
			}
			if (this.errorReporting)
			{
				stringBuilder.Append(" ERRORREPORTING=1");
			}
			else
			{
				stringBuilder.Append(" ERRORREPORTING=0");
			}
			return stringBuilder.ToString();
		}
		public bool InstallExpress()
		{
			return new Process
			{
				StartInfo = 
				{
					FileName = this.sqlExpressSetupFileLocation,
					Arguments = "/qb " + this.BuildCommandLine(),
					UseShellExecute = false
				}
			}.Start();
		}
	}
}
